package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.example.entry.User;
import com.example.feign.FeignEureKaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class IndexController {

	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	FeignEureKaClient client;

	@GetMapping("/index")
	@HystrixCommand(fallbackMethod = "defaultStores")
	public String index() {
		return restTemplate.getForObject("http://EUREKA-CLIENT/hello", String.class);
	}

	@GetMapping("/getUser/{id}")
	public User getUser(@PathVariable("id") int id) {
		return client.getUser(id);
	}

	@RequestMapping("/getList")
	public List<User> getList() {
		log.info("query Hystrix getList...");
		return client.getList();
	}

	@RequestMapping("/addUser")
	public User addUser(User user) {
		return client.addUser(user);
	}

	public String defaultStores() {
		return "请求微服务失败，返回默认数据";
	}
}
